<template>
  <el-dialog v-model="visible" :title="form.id ? '修改' : '添加'" @closed="onReset" :close-on-click-modal="false">
    <el-form :model="form" ref="formRef" label-width="100px">
      <el-form-item label="类型">
        <el-select v-model="form.type" placeholder="请选择" clearable>
          <el-option :label="item.label" :value="item.value" v-for="item in extinguisherOption"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="合同名称">
        <el-input v-model="form.contract_name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="维修机构">
        <el-input v-model="form.maintenance_organization" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="服务开始时间">
        <el-date-picker
          v-model="form.service_start_time"
          type="datetime"
          placeholder="请选择"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="服务结束时间">
        <el-date-picker
          v-model="form.service_end_time"
          type="datetime"
          placeholder="请选择"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="合同附件">
        <CustomUpload
          v-model="form.attachment"
          :limit="1"
          style="width: 100%;"
          :show-file-list="true"
          :file-list="form.fileList"
        >
          <el-button type="primary">选择文件</el-button>
        </CustomUpload>
      </el-form-item>
      <el-form-item label="合同状态">
        <el-radio-group v-model="form.contract_status">
          <el-radio :label="item.value" v-for="item in contractStatusOption">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onConfirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { guanlihetongAddOrUpdate } from '@/api/miehuoqi'
import { contractStatusOption } from '@/util/explain'
import CustomUpload from '@/components/CustomUpload/index.vue'
import { extinguisherOption, extinguisherWarnOption } from '@/util/explain'
const emit = defineEmits(['refresh'])
const visible = ref(false)
const formRef = ref()
const form = reactive({
  id: 0,
  fileList: [],
  type: '',
  contract_name: '',
  maintenance_organization: '',
  service_start_time: '',
  service_end_time: '',
  attachment: '',
  contract_status: 1
})
const resetForm = JSON.parse(JSON.stringify(form))
const open = (data?) => {
  visible.value = true
  if (data) Object.assign(form, { ...data })
}
const onConfirm = async () => {
  await formRef.value.validate()
  guanlihetongAddOrUpdate(form).then(({ msg }: any) => {
    ElMessage.success(msg)
    visible.value = false
    emit('refresh')
  })
}
const onReset = () => {
  Object.keys(resetForm).forEach((item: string) => {
    form[item] = resetForm[item]
  })
}
defineExpose({ open })
</script>

<style lang="scss" scoped></style>
